﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_Message : System.Web.UI.Page
{
    MessBUS messBUS = new MessBUS();
    DataTable tbMess = new DataTable();
    DataView view;
    protected void Page_Load(object sender, EventArgs e)
    {
        LoadDataList();
        lblError.Text = "";
    }
    protected void LoadDataList()
    {
        tbMess = messBUS.returnAllMess();
        view = new DataView(tbMess);
        grvMessage.DataSource = tbMess;
        grvMessage.DataBind();

    }

    protected void btnInsert_Click(object sender, EventArgs e)
    {
        if (txtValueMessage.Text.Equals(""))
        {
            lblError.Text = "You must enter message";
        }
        else
        {
            bool check = messBUS.insertMess(txtValueMessage.Text);
            if (check)
            {
                LoadDataList();
                lblError.Text = "Insert new message success";
            }
            else
            {
                lblError.Text = "Insert new message error";
            }
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtValueMessage.Text.Equals(""))
            {
                lblError.Text = "You must enter message";
            }
            else
            {
                bool check = messBUS.updateMess(Int32.Parse(lblID.Text), txtValueMessage.Text);
                if (check)
                {
                    LoadDataList();
                }
            }
        }catch(Exception ex){
            lblError.Text = "You must choice message";
        }
    }
    protected void grvMessage_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToString().EndsWith("DelItem"))
        {
            bool check = messBUS.delMess(Int32.Parse(e.CommandArgument.ToString()));
            if (check)
                LoadDataList();
            txtValueMessage.Text = "";
            lblID.Text = "";

        }
        if (e.CommandName.ToString().EndsWith("UpdateItem"))
        {
            view.RowFilter = "Mes_ID=" + Int32.Parse(e.CommandArgument.ToString());
            txtValueMessage.Text = view[0]["Mes_Details"].ToString();
            lblID.Text = e.CommandArgument.ToString();
        }
    }
    protected void grvMessage_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvMessage.PageIndex = e.NewPageIndex;
        LoadDataList();
    }
}